﻿#include "PageResultJudge.h"
#include "ui_PageResultJudge.h"
#include "GlobalData.h"
#include "../DatabaseMgmt/ResultJudge.h"

const char* CONST_RESULT_SAMPLE = "SAMPLE";

CPageResultJudge::CPageResultJudge(QWidget* parent /*= 0*/, Qt::WindowFlags f /*= 0*/)
	: QWidget(parent, f)
{
	init();
}

CPageResultJudge::~CPageResultJudge()
{

}

void CPageResultJudge::openTestReport()
{
	openTestResult();
}

void CPageResultJudge::savePage()
{
	saveTestResult();
}

void CPageResultJudge::clear()
{
	m_ui->teDescribe->clear();
	m_ui->rbtnNonSolid->setChecked(true);
}

void CPageResultJudge::init()
{
	m_ui = new Ui::PageResultJudge();
	m_ui->setupUi(this);
	connect(m_ui->rbtnNonSolid, SIGNAL(toggled(bool)), this, SLOT(slotNonSolidToggled(bool)));
	connect(m_ui->btnSave, SIGNAL(clicked()), this, SLOT(slotSaveJudgeResult()));

	m_ui->rbtnNonSolid->setProperty(CONST_RESULT_SAMPLE, QStringLiteral("综合分析样品的外观形态、理化特性以及参考文献资料，"
		"根据GB 34330-2017进行判断，所封样品不属于固体废物。"));
	m_ui->rbtnSolid->setProperty(CONST_RESULT_SAMPLE, QStringLiteral("样品的简单分析描述。综合分析样品的外观形态、"
		"理化特性以及参考文献资料，推断该样品为XXX。根据GB 34330-2017进行判断，所封样品属于固体废物。"));

}

void CPageResultJudge::openTestResult()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	CResultJudge judge;

	ResultJudge data;
	int ret = judge.queryByReportUid(nTestReportId, data);
	if (CONST_ERR_SUCCESS != ret)
	{
		qInfo() << QStringLiteral("打开检测结果失败：") << ret;
		return;
	}
	if (data.result_type == RT_NonSolid)
	{
		m_ui->rbtnNonSolid->setChecked(true);
	}
	else if (data.result_type == RT_Solid)
	{
		m_ui->rbtnSolid->setChecked(true);
	}

	m_ui->teDescribe->setPlainText(data.result_describe);

}


void CPageResultJudge::saveTestResult()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	CResultJudge judge;

	ResultJudge data;
	data.report_uid = nTestReportId;
	data.result_describe = m_ui->teDescribe->toPlainText();
	if (m_ui->rbtnSolid->isChecked())
	{
		data.result_type = RT_Solid;
	}
	else if (m_ui->rbtnNonSolid->isChecked())
	{
		data.result_type = RT_NonSolid;
	}

	int ret = judge.insertRow(data);
	if (CONST_ERR_SUCCESS != ret)
	{
		qInfo() << QStringLiteral("保存检测结果失败：") << ret;
		return;
	}
}

void CPageResultJudge::slotNonSolidToggled(bool checked)
{
	if (m_ui->rbtnNonSolid->isChecked())
	{
		m_ui->teDescribe->setPlainText(m_ui->rbtnNonSolid->property(CONST_RESULT_SAMPLE).toString());
	}
	else
	{
		m_ui->teDescribe->setPlainText(m_ui->rbtnSolid->property(CONST_RESULT_SAMPLE).toString());
	}
}

void CPageResultJudge::slotSaveJudgeResult()
{
	saveTestResult();
}
